package com.isaac.edu.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {


    /**
     * 在handler方法业务逻辑前执行，完成权限校验工作
     * @param request
     * @param response
     * @param handler
     * @return boolean true=放行，false终止
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession httpSession = request.getSession(false);
        if(null != httpSession && httpSession.getAttribute("uname")!= null){
            // 已登录
            System.out.println("您好:" + httpSession.getAttribute("uname"));
        }else{
            // 未登录
            String name = request.getParameter("regName");
            String password = request.getParameter("pwd");
            if("admin".equals(name) && "admin".equals(password)){
                HttpSession session = request.getSession();
                session.setAttribute("uname",name);
            }else{
                request.getSession();
                response.sendRedirect("/");
            }
        }
        return true;
    }
}
